package com.example.xiaomi_time

import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.RectF
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View

class XIaomiTime : View {

    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
    constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)

    private var mWidth: Int = 0
    private var mHeight: Int = 0
    private var mCenterX: Int = 0
    private var mCenterY: Int = 0

    private val paddingOut: Float = getdp(12)

    private var mPaint: Paint

    init {

        mPaint = Paint()
        mPaint.color = Color.BLUE
        mPaint.style = Paint.Style.STROKE
        mPaint.isAntiAlias = true
        mPaint.textSize = getsp(10)
        mPaint.strokeWidth = getdp(2)
        mPaint.textAlign = Paint.Align.CENTER
    }

    fun getdp(value: Int): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value.toFloat(), context.resources.displayMetrics)
    }

    fun getsp(value: Int): Float {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, value.toFloat(), context.resources.displayMetrics)
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val width = View.MeasureSpec.getSize(widthMeasureSpec)
        val height = View.MeasureSpec.getSize(heightMeasureSpec)
        //设置为正方形
        val imageSize = if (width < height) width else height
        setMeasuredDimension(imageSize, imageSize)
    }


    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        super.onSizeChanged(w, h, oldw, oldh)

        mWidth = w
        mHeight = h
        mCenterX = mWidth / 2
        mCenterY = mHeight / 2
    }

    override fun onDraw(canvas: Canvas?) {
        //super.onDraw(canvas)

        canvas?.translate(mCenterX.toFloat(), mCenterY.toFloat())

        drawArcCircle(canvas!!)

        drawText(canvas)

        drawLine(canvas)
    }


    private fun drawArcCircle(canvas: Canvas) {

        val min = Math.min(width, height)
        val size = (min - paddingOut) / 2

        val rect = RectF(-size, -size, size, size)

        canvas.drawArc(rect, 5f, 80f, false, mPaint)
        canvas.drawArc(rect, 95f, 80f, false, mPaint)
        canvas.drawArc(rect, 185f, 80f, false, mPaint)
        canvas.drawArc(rect, 275f, 80f, false, mPaint)
    }


    private fun drawText(canvas: Canvas) {
        val min = Math.min(width, height)
        val textRadius = (min - paddingOut) / 2

        mPaint.strokeWidth = 0f

        val fm = mPaint.fontMetrics
        val mTextHeight = Math.ceil((fm.leading - fm.ascent).toDouble())
        val textSize = (mTextHeight / 2).toFloat()

        canvas.drawText("3", textRadius, textSize, mPaint)
        canvas.drawText("9", -textRadius, textSize, mPaint)

        canvas.drawText("1", 0f, 0f + textSize, mPaint)

        //canvas.drawLine(-width.toFloat(), 0f, width.toFloat(), 0f, mPaint)

        canvas.drawText("6", 0f, (textRadius + mTextHeight / 2).toFloat(), mPaint)
        canvas.drawText("12", 0f, (-textRadius + mTextHeight / 2).toFloat(), mPaint)

    }


    private fun drawLine(canvas: Canvas) {
        val min = Math.min(width, height) / 2

        val left = min.toFloat() * 3 / 4

        mPaint.color = Color.RED

        for (i in 0 until 360 step 2) {
            canvas.save()
            canvas.translate(0f,0f)
            canvas.rotate(i.toFloat())

            canvas.drawLine(left, 0f, left + getdp(10), 0f, mPaint)

            canvas.restore()
        }
    }
}
